package com.inspur.his.controller;

import com.inspur.his.pojo.MakeRegisterOrder;
import com.inspur.his.pojo.PayDetail;
import com.inspur.his.pojo.PayStandard;
import com.inspur.his.service.MakeRegisterOrderService;
import com.inspur.his.service.PayDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 收费明细
 */
@Controller
@RequestMapping("/payDetail")
public class payDetailController {

    private int pageSize = 10;

    @Autowired
    private PayDetailService payDetailService;

    @Autowired
    private MakeRegisterOrderService makeRegisterOrderService;

    /**
     * 跳转收费明细页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "/outDepart/payDetailAdd.jsp";
    }

    /**
     * 跳转收费明细更新页面
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdateMakeRegister(Model model, Integer id) {
        PayDetail payDetail = payDetailService.getOneById(id);
        model.addAttribute("payStandard", payDetail);
        return "/outDepart/payDetailUpdate.jsp";
    }

    /**
     * 添加收费明细信息
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String add(PayDetail payDetail, Integer roId) {
        MakeRegisterOrder makeRegisterOrder = makeRegisterOrderService.getOneById(roId);
        makeRegisterOrder.setStatus(3);
        makeRegisterOrderService.update(makeRegisterOrder);
        payDetailService.add(payDetail);
        return "200";
    }

    /**
     * 更新
     * @param payDetail
     * @return
     */
    @RequestMapping("/update")
    public String update(PayDetail payDetail) {
        payDetailService.update(payDetail);
        return "redirect:/payDetail/show?currPage=1";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Integer id) {
        payDetailService.delete(id);
        return "redirect:/payDetail/show?currPage=1";
    }

    /**
     * 分页查询
     * @param model
     * @param currPage
     * @return
     */
    @RequestMapping("/show")
    public String show(Model model, String memberId, String payType, int currPage) {
        int total = payDetailService.count(memberId, payType);
        List<PayDetail> list = payDetailService.show(memberId, payType, currPage, pageSize);
        int lastPage = 1;
        if (total>10) {
            lastPage = total % pageSize == 0 ? total/pageSize : total/pageSize + 1;
        }
        model.addAttribute("total", total);
        model.addAttribute("lastPage", lastPage);
        model.addAttribute("currPage", currPage);
        model.addAttribute("payDetails", list);
        return "/outDepart/payDetail.jsp";
    }
}
